/**
 * 声音类
 * @author zhouzhanglin
 * @param filePath
 * @param autoPlay
 * @param _loop 是否循环播放，默认为false
 * @returns {Sound}
 */
Sound  =function(filePath, autoPlay, _loop)
{
	EventDispatcher.call(this);
	this._loaded = false;
    this._playing = false;
	this._autoPlay = autoPlay;
	this._loop = _loop || false;
	try
	{
		this._element = document.createElement('audio');
		this._element.preload = true;
		this._element.src = filePath;
		this._element.load();
		//fix known issue: some browsers(like GECKO) don't support Audio.loop property.
		//so add an 'ended' event listener to handle looping.
		this._element.addEventListener("ended", endedHandler, false);
		var _self = this ;
		function endedHandler(e){
			_self._endHandler = false;  	
		}
		
		this._loadInterval = setInterval(loadHandler , 10);
		//声音下载完成
		function loadHandler()
		{
			if (_self._element.readyState > 2)
			{
				_self._loaded = true;
				clearTimeout(_self._loadInterval);
				_self._loadInterval = null;
				if(_self._autoPlay) _self.play();
				_self.dispatchEvent(new Event( Event.COMPLETE));
			}
			if (_self._element.error) 
			{
				clearTimeout(_self._loadInterval);
				_self._loadInterval = null;
			}
		}
	}catch(e){};
};
FLASH.extend(Sound , EventDispatcher) ;
/**
 * 停止处理，私有
 */
Sound.prototype._endHandler = function()
{
	if(this._loop) this.play();
	else this._playing = false;
};
/**
 * 播放
 */
Sound.prototype.play = function()
{
	if (this.isLoaded())
	{
        this._element.play();
        this._playing = true;
    }else
	{
		this._autoPlay = true;
	}
};
/**
 * 停止 
 */
Sound.prototype.stop = function()
{
    if (this.isPlaying())
	{
        this._element.pause();
        this._playing = false;
    }
};
/**
 * 是否下载完成 
 * @returns
 */
Sound.prototype.isLoaded = function()
{
    return this._loaded;
};
/**
 * 是否播放
 * @returns
 */
Sound.prototype.isPlaying = function()
{
    return this._playing;
};